/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include ".\MAddressMAC.h"

//---------------------------------------------------------------------------

MAddressMAC::MAddressMAC(void)
{
	Initialize();
}

MAddressMAC::~MAddressMAC(void)
{
}

//---------------------------------------------------------------------------

void MAddressMAC::Initialize(void)
{
	memset(&addr[0], 0, 6);
}

void MAddressMAC::LoadFromPointer(MAddressMAC* macIn)
{
	int i;

	if(macIn==NULL){
		Initialize();
		return;
	}

	for(i=0; i<6; i++)
		addr[i] = macIn->addr[i];
}

void MAddressMAC::LoadFromBytePtr(BYTE* bbb)
{
	int i;

	if(bbb==NULL){
		Initialize();
		return;
	}

	for(i=0; i<6; i++)
		addr[i] = bbb[i];
}

//---------------------------------------------------------------------------

bool MAddressMAC::IsEmpty(void)
{
	if(addr[0]!=0x00) return false;
	if(addr[1]!=0x00) return false;
	if(addr[2]!=0x00) return false;
	if(addr[3]!=0x00) return false;
	if(addr[4]!=0x00) return false;
	if(addr[5]!=0x00) return false;

	return true;
}

bool MAddressMAC::IsBroadcast(void)
{
	if(addr[0]!=0xFF) return false;
	if(addr[1]!=0xFF) return false;
	if(addr[2]!=0xFF) return false;
	if(addr[3]!=0xFF) return false;
	if(addr[4]!=0xFF) return false;
	if(addr[5]!=0xFF) return false;

	return true;
}

//---------------------------------------------------------------------------

void MAddressMAC::SetAsBroadcastAddress(void)
{
	addr[0]=0xFF;
	addr[1]=0xFF;
	addr[2]=0xFF;
	addr[3]=0xFF;
	addr[4]=0xFF;
	addr[5]=0xFF;
}

//---------------------------------------------------------------------------

CString MAddressMAC::GetS(void)
{
	CString str;

	str.Format(_T("%02X:%02X:%02X:%02X:%02X:%02X"), addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
	return str;
}

CString MAddressMAC::GetSD(void)
{
	CString str;

	str.Format(_T("%02x-%02x-%02x-%02x-%02x-%02x"), addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
	return str;
}

//---------------------------------------------------------------------------

int MAddressMAC::BHtoInt(CString strIn)
{
	LPCTSTR alfa;
	int vH, vL;
	char ch;

	alfa = strIn;

	ch = alfa[0];
	if(ch>='0' && ch<='9') vH = (int)(ch - '0');
	else
		if(ch>='A' && ch<='F') vH = (int)(ch - 'A' + 10);
		else
			if(ch>='a' && ch<='f') vH = (int)(ch - 'a' + 10);
			else return -1;

	ch = alfa[1];
	if(ch>='0' && ch<='9') vL = (int)(ch - '0');
	else
		if(ch>='A' && ch<='F') vL = (int)(ch - 'A' + 10);
		else
			if(ch>='a' && ch<='f') vL = (int)(ch - 'a' + 10);
			else return -1;

	vL += 16 * vH;
	return vL;
}

int MAddressMAC::BHtoIntL(LPCTSTR alfa)
{
	int vH, vL;
	char ch;

	ch = alfa[0];
	if(ch>='0' && ch<='9') vH = (int)(ch - '0');
	else
		if(ch>='A' && ch<='F') vH = (int)(ch - 'A' + 10);
		else
			if(ch>='a' && ch<='f') vH = (int)(ch - 'a' + 10);
			else return -1;

	ch = alfa[1];
	if(ch>='0' && ch<='9') vL = (int)(ch - '0');
	else
		if(ch>='A' && ch<='F') vL = (int)(ch - 'A' + 10);
		else
			if(ch>='a' && ch<='f') vL = (int)(ch - 'a' + 10);
			else return -1;

	vL += 16 * vH;
	return vL;
}

bool MAddressMAC::LoadFromString(CString strIn)
{
	CString str;
	LPCTSTR alfa;
	bool ok;
	int i, val;

	str = strIn.Trim();
	if(str.GetLength()!=17)
		return false;

	///< check separators
	alfa = str;
	ok   = false;
	if(alfa[2]==alfa[5] && alfa[5]==alfa[8] && alfa[8]==alfa[11] && alfa[11]==alfa[14] && alfa[14]=='-') ok = true;
	if(alfa[2]==alfa[5] && alfa[5]==alfa[8] && alfa[8]==alfa[11] && alfa[11]==alfa[14] && alfa[14]==':') ok = true;
	if(!ok) return false;

	///< load values
	for(i=0; i<6; i++){
		val = BHtoIntL(&alfa[3*i]);
		if(val<0 || val>255) return false;
		addr[i] = (BYTE)val;
	}

	return true;
}

//---------------------------------------------------------------------------

int MAddressMAC::Compare(MAddressMAC* macIn)
{
	int i;

	if(macIn==NULL) return 1;

	for(i=0; i<6; i++){
		if(addr[i] < macIn->addr[i]) return -1;
		if(addr[i] > macIn->addr[i]) return 1;
	}

	return 0;
}

bool MAddressMAC::IsEqualWith(MAddressMAC* macIn)
{
	if(macIn==NULL) return false;

	if(addr[0]!=macIn->addr[0]) return false;
	if(addr[1]!=macIn->addr[1]) return false;
	if(addr[2]!=macIn->addr[2]) return false;
	if(addr[3]!=macIn->addr[3]) return false;
	if(addr[4]!=macIn->addr[4]) return false;
	if(addr[5]!=macIn->addr[5]) return false;

	return true;
}

//---------------------------------------------------------------------------

bool MAddressMAC::IsBroadcastAddress(void)
{
	int i;

	for(i=0; i<6; i++)
		if(addr[i]!=0xFF) return false;

	return true;
}

//---------------------------------------------------------------------------

bool MAddressMAC::IsMulticastAddress(void)
{
	if(addr[0] & 0x01) return true;

	return false;
}

//---------------------------------------------------------------------------

bool MAddressMAC::IsGvrpDestinationMAC(void)
{
	cmpAddr[0] = 0x01;
	cmpAddr[1] = 0x80;
	cmpAddr[2] = 0xC2;
	cmpAddr[3] = 0x00;
	cmpAddr[4] = 0x00;
	cmpAddr[5] = 0x21;

	return memcmp(&cmpAddr[0], &addr[0], 6)==0 ? true : false;
}

bool MAddressMAC::IsGmrpDestinationMAC(void)
{
	cmpAddr[0] = 0x01;
	cmpAddr[1] = 0x80;
	cmpAddr[2] = 0xC2;
	cmpAddr[3] = 0x00;
	cmpAddr[4] = 0x00;
	cmpAddr[5] = 0x20;

	return memcmp(&cmpAddr[0], &addr[0], 6)==0 ? true : false;
}

bool MAddressMAC::IsStpDestinationMAC(void)
{
	cmpAddr[0] = 0x01;
	cmpAddr[1] = 0x80;
	cmpAddr[2] = 0xC2;
	cmpAddr[3] = 0x00;
	cmpAddr[4] = 0x00;
	cmpAddr[5] = 0x00;

	return memcmp(&cmpAddr[0], &addr[0], 6)==0 ? true : false;
}

//---------------------------------------------------------------------------
